﻿#ifndef CLOUDDEVICE_H
#define CLOUDDEVICE_H

#include "Device/BaseDevice.h"
#include "Protocol/CloudProtocol.h"
#include "System/System.h"
#include "DataType/CurrentPointStore.h"
#include"DataType/SavePointStruct.h"
#include "Encrypt/Encrypt.h"
#include <QTimer>
#include <QDateTime>
#include <QElapsedTimer>
#include "Command/CloudProtocol/ResponseCmd.h"
#include "Command/CloudProtocol/HandshakeCmd.h"
#include "Command/CloudProtocol/HandshakeSucceedCmd.h"
#include "Command/CloudProtocol/HeartbeatCmd.h"
#include "Command/CloudProtocol/GatewayLoginCmd.h"
#include "Command/CloudProtocol/GatewayLoginSucceedCmd.h"
#include "Command/CloudProtocol/DownloadPointListCmd.h"
#include "Command/CloudProtocol/DownloadPointListRequestCmd.h"
#include "Command/CloudProtocol/DownloadCustomSceneCmd.h"
#include "Command/CloudProtocol/DownloadCustomSceneRequestCmd.h"
#include "Command/CloudProtocol/UpdateCustomSceneCmd.h"
#include "Command/CloudProtocol/CustomSceneControlCmd.h"
#include "Command/CloudProtocol/DownloadAutomaticRequestCmd.h"
#include "Command/CloudProtocol/DownloadAutomaticCmd.h"
#include "Command/CloudProtocol/UpdateAutomaticCmd.h"
#include "Command/CloudProtocol/DownloadFileCmd.h"
#include "Command/CloudProtocol/DownloadFileRequestCmd.h"
#include "Command/CloudProtocol/StateSyncCmd.h"
#include "Command/CloudProtocol/RebootCmd.h"
#include "Command/CloudProtocol/HeartBestcmd.h"
#include "Command/CloudProtocol/GatewayUserStataCmd.h"
#include "Command/CloudProtocol/QueryOtherEquipmentCmd.h"

#include "Command/ZigbeeProtocol/RestartZigbeeCmd.h"
#include "Command/ZigbeeProtocol/EquipmentIntoNetCmd.h"
#include "Command/ZigbeeProtocol/ReceiveControlPointListCmd.h"
#include "Command/ZigbeeProtocol/SendControlPointListCmd.h"
#include"Command/ZigbeeProtocol/SetNewZigbeeNetCmd.h"
#include "Command/ZigbeeProtocol/Zigbee3NewJoinParamCmd.h"
#include"Command/ZigbeeProtocol/Zigbee3ControlPointListCmd.h"
#include "Command/ZigbeeProtocol/Zigbee3QueryBackCmd.h"
#include"Command/Version2Protocol/SynchroSystemCmd.h"

#include "Command/CloudProtocol/CloudControlPointCmd.h"
#include "Command/CloudProtocol/UpdatePointCmd.h"
#include "Command/CloudProtocol/DownloadLogFile.h"

#include "Command/CloudProtocol/DownloadFileResultCmd.h"

#include"Command/CloudProtocol/PhoneToSetZB3cmd.h"
#include"Command/CloudProtocol/PhoneToAllocationCmd.h"
#include "Function/DownloadFileResultFunc.h"
#include "Function/ControlPointFunc.h"
#include "Function/UpdatePointFunc.h"
#include "Function/DownloadFileFunc.h"
#include"Function/Rs485SetTime.h"
#include "File/Xml/MacXml.h"
#include "File/Xml/UserListXml.h"
#include "File/Xml/CustomSceneXml.h"
#include "File/Xml/AutomaticXml.h"
#include "File/Xml/Zigbee3InfraredXml.h"
#include "File/Xml/SavePointXml.h"
#include "File/Xml/OtherEquipmentXml.h"

class CloudDevice : public BaseInterface<CloudDevice>
{
    Q_OBJECT
public:
    Q_INVOKABLE explicit CloudDevice(QObject *parent = 0);

private:

    CloudProtocol *cloudProtocol;
    PortStruct m_target;
    QTimer *heartbeatTimer;

    quint8  m_heartbeatTimeoutCount;
    quint32 m_heartbeatIntervalTime;
    quint32 m_heartbeatTimeoutTime;

    bool m_isLoginSucceed;

    bool isLegalClient(PortStruct source);
    void responseCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void handshakeSucceedCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void heartBeatCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void gatewayLoginSucceedCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void controlPointCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void downloadPointListCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void downloadCustomSceneCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void updateCustomSceneCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void customSceneControlCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void downloadAutomaticCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void updateAutomaticCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void updatePointCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void downloadFileCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void rebootCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void downloadLogFileCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void phoneToallocationCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd);
    void heartbeatCmdTimeoutHandle(PortStruct target,QSharedPointer<BaseCmd> sharedCmd);

    void updatePointFuncHandle(QSharedPointer<BaseFunc> sharedFunc);
    void downloadFileResultFuncHandle(QSharedPointer<BaseFunc> sharedFunc);
    void RS485setTimeFuncHandle(QSharedPointer<BaseFunc> sharedFunc);
    void reconnectTcpSrever();
    //void heartBestHandle(PortStruct target,QSharedPointer<BaseCmd> sharedCmd);
    void heartBestHandled(PortStruct target,QSharedPointer<BaseCmd> sharedCmd);
public slots:
    void isConnectedSlot(bool isConnect,PortStruct target);
    void heartbeatSlot();

};

#endif // CLOUDDEVICE_H
